/**
 * @author helio
 */
var BankAccountManageView = Ext.extend(BasicPanel,{
	inputWin : null,
	
	initlize : function() {
		
		if(!this.detailsGrid) {
						
			this.detailsStore = new Ext.data.Store({
			    proxy: new Ext.ux.data.PagingMemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'accounts',
					remoteSort: true,
			        fields: ['id','code','name', 'bank', 'classification', 'comment','default_flag']
			    })
			});
			
			// create paging bar
			var pagingBar = new Ext.PagingToolbar({
		        pageSize: 25,
		        store: this.detailsStore,
		        displayInfo: true,
				
				plugins: new Ext.ux.ProgressBarPager()
		    });
			
			// create the grid
			var csm = new Ext.grid.CheckboxSelectionModel();
			
			this.detailsGrid = new Ext.grid.GridPanel({
				store: this.detailsStore,
				columns: [csm, {
					header: "开户行",
					dataIndex: 'bank',
					sortable: true
				}, {
					header: "帐号",
					dataIndex: 'code',
					sortable: true
				}, {
					header: "帐户名称",
					dataIndex: 'name',
					sortable: true
				}, {
					header: "银行存款科目",
					dataIndex: 'classification',
					sortable: true,
					width: 80
				}],
				
				sm: csm,
				
				tbar:[{
		            text:'添加新银行帐户',
		            tooltip:'添加新银行帐户',
		            iconCls:'add',
					handler: this.newBankAccount.createDelegate(this)
		        }, '-', {
		            text:'修改银行帐户',
		            tooltip:'修改银行帐户',
		            iconCls:'option',
					handler: this.modifyBankAccount.createDelegate(this)
		        },'-',{
		            text:'删除选中的帐户',
		            tooltip:'删除选中的银行帐户',
		            iconCls:'remove',
					handler: this.deleteSelectedBankAccount.createDelegate(this)
		        }],
				
				bbar: pagingBar,
				
				viewConfig: {
			        forceFit: true,
					enableRowBody:true,
		            showPreview:true
			    },
		
				loadMask: true,
				stripeRows: true,
				border: false,
				autoExpandColumn: 'code',
				layout: 'fit',
				
				listeners: {
					cellclick : function(grid, rowIndex, columnIndex, e) {
						this.selectedRowIndex = rowIndex;
					}.createDelegate(this),
					
					rowdblclick : this.modifyBankAccount.createDelegate(this)
				}
			});
		}
	},
	
	initlizeDataHandler : function(resultJson) {
		this.detailsJson['accounts'] = resultJson.accounts;
		this.detailsStore.load({params:{start:0, limit:25}});
	},
	
	getCorePanel : function() {
		if (!this.detailsGrid) {
			this.initlize();
			SALE.Action.getBankAccountDetailsJson(this.initlizeDataHandler.createDelegate(this));
		}
		return this.detailsGrid;
	},
	
	showEditeWin : function() {
		
		if(!this.inputWin) {
				
				this.inputForm = new Ext.FormPanel({
	 				frame: true,
					labelWidth: 55,
					defaultType: 'textfield',
					
					frame: true,
					
					items: [{
						xtype:'fieldset',
						title: '基本信息',
						autoHeight:true,
					    anchor:'100%',
						layout:'form',
								
						items: [{
							xtype:'textfield',
							fieldLabel: '开户银行',
			                name: 'bank',
			                anchor:'95%'
						}, {
					    	xtype:'textfield',
					        fieldLabel: '帐号',
					        name: 'code',
					        anchor:'80%'
					    }, {
							xtype:'textfield',
							fieldLabel: '帐户名称',
			                name: 'name',
			                anchor:'95%'		
						}, new Ext.form.ComboBox({
								tpl: comboxTpl,
								fieldLabel: '存款科目',
						        store: DataStoreUtil.getClassificationsStore(),
						        displayField:'name',
						        typeAhead: true,
						        mode: 'local',
						        forceSelection: false,
						        triggerAction: 'all',
						        emptyText:'输入银行存款科目...',
						        selectOnFocus:true,
								name: 'classification',
								
								anchor:'80%'
						}), {
							xtype:'checkbox',
							boxLabel: '设置为默认帐户', 
			                name: 'default_flag',
			                anchor:'100%'		
						}]
					}],
							
					buttons: [{
			            text: '保存',
						listeners: {
							"click": function(){
								this.synchronizeJson();
								
								if(this.formDataJson.code && this.formDataJson.code != "") {
									this.saveBankAccountInfo();
								}
								
							}.createDelegate(this)
						}
			        },{
			            text: '取消',
						listeners:{
							"click":function(){
								this.inputForm.form.reset();
							}.createDelegate(this)
						}
			        }]
				});
				
		 		this.inputWin = new Ext.Window({
		 		
		 			title: '银行帐户添加/编辑',
					closable: true,				
					//maximizable: true,
					collapsible  : true,
					width: 400,
					height: 268,
					plain: true,
					layout: 'fit',
					closeAction :'hide',
					
					items: this.inputForm,
					
					listeners: {
						'show': function() {
							this.synchronizeForm();			
						}.createDelegate(this),
						
						'hide': function() {							
							this.clear();
						}.createDelegate(this)
					}
		 		});			
			}
		
		this.inputWin.show();
	},
	
	newBankAccount : function() {
		this.task = "new";		
		this.showEditeWin();
	},
	
	modifyBankAccount : function() {
		var recordList = this.detailsGrid.getSelectionModel().getSelections();
		if(recordList.length == 0) {
			SALE.Utils.showMessageBox('请先选择一条数据！');
		} else if(recordList.length > 1) {
			SALE.Utils.showMessageBox('每次只能编辑一条数据，请确认只选中了一条数据！');
		} else {
			var id = recordList[0].get("id");
			
			SALE.Action.searchBankAccountJson(this.showModifyDataHandler.createDelegate(this), id);
		}
	},
	
	deleteSelectedBankAccount : function() {
		this.selectedRecordList = this.detailsGrid.getSelectionModel().getSelections();
		if(this.selectedRecordList.length == 0) {
			SALE.Utils.showMessageBox('请选择你要删除的数据！');
		} else {
			Ext.MessageBox.confirm('删除确认', '你真的要删除选中的商品信息吗?', function(btn) {
				if (btn == "yes") {
					SALE.Action.deleteBankAccountInfo(this.deleteSuccessHandler.createDelegate(this), this.selectedRecordList);
				}
			}.createDelegate(this));
		}
	},
	
	saveBankAccountInfo : function() {
		SALE.Action.saveBankAccountInfo(this.saveSuccessHandler.createDelegate(this), this.formDataJson, this.task);
	}
});

function showBankAccountManagePanel(){
	var  manageView = new BankAccountManageView();
	
	SALE.App.showOperateTabPanel(manageView.getCorePanel(), "银行帐户管理");
}